home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / util / fsr / fsr.c < prev    next >
Text File  |  1992-11-24  |  7KB  |  231 lines

  1. /********************************************************************
  2.   FSR displays the percentage of free system resources in real time.
  3.   Copyright (c) 1993 Jeff Prosise. First published in PC Magazine,
  4.   U.S. Edition, April 13, 1993.
  5.  ********************************************************************/
  6.  
  7. #include <windows.h>
  8. #include <stdlib.h>
  9.  
  10. #define IDM_ALWAYSONTOP 100
  11.  
  12. extern DWORD FAR PASCAL GetHeapSpaces (HMODULE);
  13. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  14. DWORD GetFSR (BOOL);
  15.  
  16. WORD wTimerID;                        // Timer ID
  17.  
  18. /********************************************************************
  19.   WinMain starts the program.
  20.  ********************************************************************/
  21.  
  22. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  23.                     LPSTR lpszCmdLine, int nCmdShow)
  24. {
  25.     static char szAppName[] = "Free System Resources";
  26.     WNDCLASS wndclass;
  27.     HWND hwnd;
  28.     MSG msg;
  29.  
  30.      if (hPrevInstance)
  31.         return FALSE;
  32.  
  33.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  34.     wndclass.lpfnWndProc = (WNDPROC) WndProc;
  35.     wndclass.cbClsExtra = 0;
  36.     wndclass.cbWndExtra = 0;
  37.     wndclass.hInstance = hInstance;
  38.     wndclass.hIcon = NULL;
  39.     wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  40.     wndclass.hbrBackground = GetStockObject (LTGRAY_BRUSH);
  41.     wndclass.lpszMenuName = NULL;
  42.     wndclass.lpszClassName = szAppName;
  43.  
  44.     RegisterClass (&wndclass);
  45.  
  46.     hwnd = CreateWindow (szAppName, szAppName, WS_OVERLAPPEDWINDOW,
  47.             CW_USEDEFAULT, CW_USEDEFAULT, 384, 96, NULL, NULL,
  48.             hInstance, NULL);
  49.  
  50.     if ((wTimerID = SetTimer (hwnd, 1, 1000, NULL)) == NULL) {
  51.         MessageBox (hwnd, "Unable to allocate a timer", "Error",
  52.             MB_ICONEXCLAMATION | MB_OK);
  53.         return FALSE;
  54.     }
  55.  
  56.      ShowWindow (hwnd, nCmdShow);
  57.     UpdateWindow (hwnd);
  58.  
  59.      while (GetMessage (&msg, NULL, 0, 0))
  60.             DispatchMessage (&msg);
  61.  
  62.      return msg.wParam;
  63. }
  64.  
  65. /********************************************************************
  66.   WndProc processes messages to the main window.
  67.  ********************************************************************/
  68.  
  69. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  70. {
  71.     static char szSection[] = "Options";
  72.     static char szEntry[] = "AlwaysOnTop";
  73.     static char szIniFile[] = "FSR.INI";
  74.  
  75.     static HPEN hGrayPen;
  76.     static HBRUSH hPinkBrush, hRedBrush;
  77.     RECT rect, rectUsed, rectFree;
  78.     static DWORD dwPercentFree;
  79.     DWORD dwVersion, dwTotal;
  80.     static BOOL bWin31Flag;
  81.     static HMENU hSysMenu;
  82.     char szBuffer[4];
  83.     PAINTSTRUCT ps;
  84.     HDC hdc;
  85.  
  86.     switch (message) {
  87.  
  88.     case WM_CREATE:
  89.         dwVersion = GetVersion ();
  90.         if ((LOBYTE (LOWORD (dwVersion)) >= 3) &&
  91.             (HIBYTE (LOWORD (dwVersion)) >= 10))
  92.             bWin31Flag = TRUE;
  93.         else
  94.             bWin31Flag = FALSE;
  95.  
  96.         hGrayPen = CreatePen (PS_SOLID, 1, RGB (128, 128, 128));
  97.         hPinkBrush = CreateSolidBrush (RGB (192, 0, 192));
  98.         hRedBrush = CreateSolidBrush (RGB (192, 0, 0));
  99.  
  100.         dwPercentFree = GetFSR (bWin31Flag);
  101.  
  102.         if (bWin31Flag) {
  103.             hSysMenu = GetSystemMenu (hwnd, FALSE);
  104.             AppendMenu (hSysMenu, MF_SEPARATOR, 0, NULL);
  105.             AppendMenu (hSysMenu, MF_STRING, IDM_ALWAYSONTOP,
  106.                 "Always on &Top");
  107.             if (GetPrivateProfileInt (szSection, szEntry, 0, szIniFile)) {
  108.                 CheckMenuItem (hSysMenu, IDM_ALWAYSONTOP, MF_CHECKED);
  109.                 SetWindowPos (hwnd, HWND_TOPMOST, 0, 0, 0, 0,
  110.                     SWP_NOMOVE | SWP_NOSIZE);
  111.             }
  112.         }
  113.         return 0;
  114.  
  115.     case WM_PAINT:
  116.         hdc = BeginPaint (hwnd, &ps);
  117.         SetBkMode (hdc, TRANSPARENT);
  118.         GetClientRect (hwnd, &rect);
  119.  
  120.         if (IsIconic (hwnd)) {
  121.             FillRect (hdc, &rect, GetStockObject (LTGRAY_BRUSH));
  122.             SetTextColor (hdc, RGB (0, 0, 192));
  123.             DrawText (hdc, _itoa ((int) dwPercentFree, szBuffer, 10),
  124.                 -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  125.         }
  126.         else {
  127.             if ((--rect.bottom < 16) || (--rect.right < 32))
  128.                 return 0;
  129.             InflateRect (&rect, -4, -4);
  130.  
  131.             SelectObject (hdc, hGrayPen);
  132.             MoveTo (hdc, rect.left, rect.bottom);
  133.             LineTo (hdc, rect.left, rect.top);
  134.             LineTo (hdc, rect.right+1, rect.top);
  135.             MoveTo (hdc, rect.right-1, rect.top+1);
  136.             LineTo (hdc, rect.left+1, rect.top+1);
  137.             LineTo (hdc, rect.left+1, rect.bottom);
  138.  
  139.             SelectObject (hdc, GetStockObject (WHITE_PEN));
  140.             MoveTo (hdc, rect.left+1, rect.bottom);
  141.             LineTo (hdc, rect.right, rect.bottom);
  142.             LineTo (hdc, rect.right, rect.top);
  143.             MoveTo (hdc, rect.right-1, rect.top+2);
  144.             LineTo (hdc, rect.right-1, rect.bottom-1);
  145.             LineTo (hdc, rect.left+1, rect.bottom-1);
  146.  
  147.             InflateRect (&rect, -2, -2);
  148.             rect.right++;
  149.             rect.bottom++;
  150.             CopyRect (&rectUsed, &rect);
  151.             CopyRect (&rectFree, &rect);
  152.             dwTotal = (DWORD) (rect.right - rect.left);
  153.             rectFree.right = rectFree.left +
  154.                 ((int) ((dwTotal * dwPercentFree) / 100));
  155.             rectUsed.left = rectFree.right;
  156.  
  157.             if (dwPercentFree >= 25)
  158.                 FillRect (hdc, &rectFree, hPinkBrush);
  159.             else
  160.                 FillRect (hdc, &rectFree, hRedBrush);
  161.             FillRect (hdc, &rectUsed, GetStockObject (LTGRAY_BRUSH));
  162.             SetTextColor (hdc, RGB (255, 255, 255));
  163.             DrawText (hdc, _itoa ((int) dwPercentFree, szBuffer, 10),
  164.                 -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  165.         }
  166.  
  167.         EndPaint (hwnd, &ps);
  168.         return 0;
  169.  
  170.     case WM_TIMER:
  171.         dwPercentFree = GetFSR (bWin31Flag);
  172.         GetClientRect (hwnd, &rect);
  173.         InflateRect (&rect, -6, -6);
  174.         InvalidateRect (hwnd, &rect, FALSE);
  175.         return 0;
  176.  
  177.     case WM_SYSCOMMAND:
  178.         if (wParam == 100) {
  179.             if ((GetMenuState (hSysMenu, IDM_ALWAYSONTOP, MF_BYCOMMAND) &
  180.                 MF_CHECKED)) {
  181.                 CheckMenuItem (hSysMenu, IDM_ALWAYSONTOP, MF_UNCHECKED);
  182.                 SetWindowPos (hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
  183.                     SWP_NOMOVE | SWP_NOSIZE);
  184.                 WritePrivateProfileString (szSection, szEntry, "0",
  185.                     szIniFile);
  186.             }
  187.             else {
  188.                 CheckMenuItem (hSysMenu, IDM_ALWAYSONTOP, MF_CHECKED);
  189.                 SetWindowPos (hwnd, HWND_TOPMOST, 0, 0, 0, 0,
  190.                     SWP_NOMOVE | SWP_NOSIZE);
  191.                 WritePrivateProfileString (szSection, szEntry, "1",
  192.                     szIniFile);
  193.             }
  194.             return 0;
  195.         }
  196.         break;
  197.  
  198.     case WM_DESTROY:
  199.         KillTimer (hwnd, wTimerID);
  200.         DeleteObject (hPinkBrush);
  201.         DeleteObject (hRedBrush);
  202.         DeleteObject (hGrayPen);
  203.         PostQuitMessage (0);
  204.         return 0;
  205.     }
  206.     return DefWindowProc (hwnd, message, wParam, lParam);
  207. }
  208.  
  209. /********************************************************************
  210.   GetFSR returns a value between 0 and 100 that quantifies the
  211.   percentage of free system resources.
  212.  ********************************************************************/
  213.  
  214. DWORD GetFSR (BOOL bWin31)
  215. {
  216.     DWORD dwHeapSpaces;
  217.     WORD wUSERPercentFree, wGDIPercentFree;
  218.  
  219.     if (bWin31)
  220.         return ((DWORD) GetFreeSystemResources (GFSR_SYSTEMRESOURCES));
  221.     else {
  222.         dwHeapSpaces = GetHeapSpaces (GetModuleHandle ("USER"));
  223.         wUSERPercentFree = (WORD) (((DWORD) (LOWORD (dwHeapSpaces)) * 100) /
  224.             ((DWORD) (HIWORD (dwHeapSpaces))));
  225.         dwHeapSpaces = GetHeapSpaces (GetModuleHandle ("GDI"));
  226.         wGDIPercentFree = (WORD) (((DWORD) (LOWORD (dwHeapSpaces)) * 100) /
  227.             ((DWORD) (HIWORD (dwHeapSpaces))));
  228.         return ((DWORD) min (wUSERPercentFree, wGDIPercentFree));
  229.     }
  230. }
  231.